﻿from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask import Flask, g, render_template, request,flash,redirect,url_for
import json
def create_app():
    app = Flask(__name__)
    # app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db/test1.db'
    app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:123456@localhost:3306/renren_fast'
    # app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:123456@localhost:3306/test1'
    app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
    app.config['SQLALCHEMY_ECHO'] = True
    return app
app=create_app()
db=SQLAlchemy(app)

		
		
class Category(db.Model):
	__tablename__  = "blog_category"
	id = db.Column(db.Integer, primary_key=True)
	name = db.Column(db.String(255), unique=True)
class Tags(db.Model):
	__tablename__  = "blog_tags"
	id = db.Column(db.Integer, primary_key=True)
	name = db.Column(db.String(255), unique=True)
class Article(db.Model):
	__tablename__  = "blog_article"
	id = db.Column(db.Integer, primary_key=True)
	title = db.Column(db.String(255), unique=True)
	author = db.Column(db.String(255))
	visited = db.Column(db.Integer)
	img = db.Column(db.String(255))
	abstract = db.Column(db.String(255))
	content = db.Column(db.Text)
	category = db.Column(db.Integer,db.ForeignKey('blog_category.id'))
	tags = db.Column(db.Integer,db.ForeignKey('blog_tags.id'))
	created_at = db.Column(db.DateTime)
	modified_at = db.Column(db.DateTime)
class Comment(db.Model):
	__tablename__  = "blog_comment"
	id = db.Column(db.Integer, primary_key=True)
	article = db.Column(db.Integer,db.ForeignKey('blog_article.id'))
	comment = db.Column(db.Text)
	created_at = db.Column(db.DateTime)
	
		
class Person(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(255), unique=True)
    birth_date = db.Column(db.DateTime)
    def __repr__(self):
        d={}
        for k in ['id','name','birth_date']:
            if getattr(self,k):d[k]=getattr(self,k)
        print(d)
        # d={'id':self.id,'name':self.name,'birth_date':self.birth_date}
        return json.dumps(d)
    def _dump(self):
        # import inspect
        # from types import FunctionType, MethodType
        # keys=[k for k,v in Person.__dict__.items() if not k.startswith('_')]
        # keys=[k for k in Person.__dict__.keys() if  not k.startswith('_') and isinstance(k, MethodType)]
        # keys=[k for k in Person.__dict__.keys() if  isinstance(k, FunctionType)]
        # keys=[k for k in Person.__dict__.keys()]
        # keys=[k for k in Person.func_closure]
        # print(keys)
        d={}
        for k in ['id','name','birth_date']:
            if getattr(self,k):d[k]=getattr(self,k)
        print(d)
        return json.dumps(d)		
def createPerson():
    Person1 = Person(name='wang',  birth_date='dfgdfgwerwer')
    Person2 = Person(name='zhang', birth_date='sdfsdfsdf')
    db.session.add_all([Person1, Person2])
    db.session.commit()
def deletePerson():
    person = Person.query.filter_by(id=1).first()
    db.session.delete(person)
    db.session.commit()
def updatePerson():
    person = Person.query.filter_by(id=1).first()
    person.birth_date = '123567'
    db.session.commit()
def selectPerson():
    personlist = Person.query.all()
    person = Person.query.filter_by(id=1).first()
    person = Person.query.filter_by(name='wang').first()
    person = Person.query.filter(Person.name == 'wang').first()

def init_db():
	# print(dir(db))
	# db.drop_all()
	db.create_all()
	# createPerson() 
if __name__ == '__main__':
    init_db()
    